package com.google.project.client.Sharing;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.SimplePager;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HasAlignment;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.datepicker.client.DateBox;
import com.google.gwt.view.client.ListDataProvider;
import com.google.project.client.Service.SpecializeService;
import com.google.project.client.Service.SpecializeServiceAsync;
import com.google.project.client.Service.TaskRequireService;
import com.google.project.client.Service.TaskRequireServiceAsync;
import com.google.project.shared.ProjectDTO;
import com.google.project.shared.ProjectTaskDTO;
import com.google.project.shared.TaskRequireDTO;

public class TaskReqDialog extends DialogBox {

	private static final Binder binder = GWT.create(Binder.class);
	@UiField
	SimplePager pager;
	@UiField(provided = true)
	CellTable<TaskRequireDTO> ctbTaskReqList = new CellTable<TaskRequireDTO>();
	@UiField
	Label lblProjectName;
	@UiField
	Label lblTaskName;
	@UiField
	DateBox dbFromDate;
	@UiField
	DateBox dbToDate;
	@UiField
	Button btnClose;
	interface Binder extends UiBinder<Widget, TaskReqDialog> {
	}

	private final TaskRequireServiceAsync taskReqSv = GWT
			.create(TaskRequireService.class);

	List<TaskRequireDTO> listTaskReqDTO = new ArrayList<TaskRequireDTO>();
	ProjectTaskDTO currTask = new ProjectTaskDTO();
	ProjectDTO currProject = new ProjectDTO();
	public TaskReqDialog() {
		setWidget(binder.createAndBindUi(this));
	}
	// contructor
	public TaskReqDialog(ProjectTaskDTO currTask, ProjectDTO currProject) {
		initTable();
		setWidget(binder.createAndBindUi(this));
		this.currProject = currProject;
		this.currTask = currTask;
		dbFromDate.setFormat(new DateBox.DefaultFormat(DateTimeFormat
				.getFormat("dd/MM/yyyy")));
		dbToDate.setFormat(new DateBox.DefaultFormat(DateTimeFormat
				.getFormat("dd/MM/yyyy")));
		lblProjectName.setText(currProject.getProjectName());
		lblTaskName.setText(currTask.getTaskName());
		dbFromDate.setValue(new Date(currTask.getStartDate()));
		dbToDate.setValue(new Date(currTask.getDueDate()));
		bindData();
		// LoadData(currTask.get);
	}
	public void bindData(){
		taskReqSv.getTaskReqByTaskId(currTask.getId(),
				new AsyncCallback<List<TaskRequireDTO>>() {

					@Override
					public void onFailure(Throwable caught) {

					}

					@Override
					public void onSuccess(List<TaskRequireDTO> result) {
						if (result != null) {
							listTaskReqDTO = result;
						}
						LoadData(listTaskReqDTO);

					}
				});
	}


	// init ctbReportTaskList
	public void initTable() {

		TextColumn<TaskRequireDTO> indexColumn = new TextColumn<TaskRequireDTO>() {
			@Override
			public String getValue(TaskRequireDTO taskReq) {
				return taskReq.getIndex() + "";
			}
		};
		TextColumn<TaskRequireDTO> specColumn = new TextColumn<TaskRequireDTO>() {
			@Override
			public String getValue(TaskRequireDTO taskReq) {
				return taskReq.getSpecializeName();
			}
		};
		TextColumn<TaskRequireDTO> expColumn = new TextColumn<TaskRequireDTO>() {
			@Override
			public String getValue(TaskRequireDTO taskReq) {
				int exp = taskReq.getExperience();
				String result = "";
				if (exp == 1) {
					result += " < 1 năm";
				} else if (exp == 2) {
					result += "1-3 năm";
				} else if (exp == 3) {
					result += "3-5 năm";
				} else if (exp == 4) {
					result += "5-10 năm";
				} else if (exp == 5) {
					result += " > 10 năm";
				}
				return result;
			}
		};
		TextColumn<TaskRequireDTO> NumEmpColumn = new TextColumn<TaskRequireDTO>() {
			@Override
			public String getValue(TaskRequireDTO taskReq) {
				return taskReq.getNumOfEmp() + " (Nhân viên)";
			}
		};
		ctbTaskReqList.setStyleName("ttable");
		ctbTaskReqList.addColumn(indexColumn, "STT");
		ctbTaskReqList.getColumn(ctbTaskReqList.getColumnIndex(indexColumn))
				.setHorizontalAlignment(HasAlignment.ALIGN_CENTER);

		ctbTaskReqList.addColumn(specColumn, "Chuyên môn");
		ctbTaskReqList.addColumn(expColumn, "Kinh nghiệm");
		ctbTaskReqList.addColumn(NumEmpColumn, "Số lượng");

	}

	// load data
	protected void LoadData(List<TaskRequireDTO> taskReqList) {
		ListDataProvider<TaskRequireDTO> dataProvider = new ListDataProvider<TaskRequireDTO>();

		// Connect the table to the data provider.
		dataProvider.addDataDisplay(ctbTaskReqList);

		// Add the data to the data provider, which automatically pushes it to
		// the widget
		int index = 1;
		List<TaskRequireDTO> list = dataProvider.getList();
		for (TaskRequireDTO taskReq : taskReqList) {
			taskReq.setIndex(index);
			list.add(taskReq);
			index++;
		}
		// SimplePager
		pager.setDisplay(ctbTaskReqList);
		pager.setPageSize(5);
	}

	@UiHandler("btnClose")
	void onBtnCloseClick(ClickEvent event) {
		this.hide();
	}

	public void closeDialog() {
		this.hide();
	}
}
